package com.ruoyi.project.manage.animal.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.manage.animal.domain.DwRareAnimal;
import com.ruoyi.project.manage.animal.service.IDwRareAnimalService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 珍奇动物Controller
 * 
 * @author mhg
 * @date 2021-10-04
 */
@Controller
@RequestMapping("/manage/animal")
public class DwRareAnimalController extends BaseController
{
    private String prefix = "manage/animal";

    @Value("${my_upload_path}")
    private String dirPath;

    @Autowired
    private IDwRareAnimalService dwRareAnimalService;

    @RequiresPermissions("manage:animal:view")
    @GetMapping()
    public String animal()
    {
        return prefix + "/animal";
    }

    /**
     * 查询珍奇动物列表
     */
    @RequiresPermissions("manage:animal:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DwRareAnimal dwRareAnimal)
    {
        startPage();
        List<DwRareAnimal> list = dwRareAnimalService.selectDwRareAnimalList(dwRareAnimal);
        return getDataTable(list);
    }

    /**
     * 导出珍奇动物列表
     */
    @RequiresPermissions("manage:animal:export")
    @Log(title = "珍奇动物", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DwRareAnimal dwRareAnimal)
    {
        List<DwRareAnimal> list = dwRareAnimalService.selectDwRareAnimalList(dwRareAnimal);
        ExcelUtil<DwRareAnimal> util = new ExcelUtil<DwRareAnimal>(DwRareAnimal.class);
        return util.exportExcel(list, "珍奇动物数据");
    }

    /**
     * 新增珍奇动物
     */
    @GetMapping("/add")
    public String add()
    {
        logger.info("走了get方法了");
        return prefix + "/add";
    }

    /**
     * 新增保存珍奇动物
     */
    @RequiresPermissions("manage:animal:add")
    @Log(title = "珍奇动物", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave( String name, String intruduce,MultipartFile file) throws IOException {
        logger.info("文件上传路径为--："+ dirPath);
        DwRareAnimal dwRareAnimal = new DwRareAnimal();
        dwRareAnimal.setName(name);
        dwRareAnimal.setIntruduce(intruduce);
        //保存文件到相关路径
        //得到原始文件名
        String fileName = file.getOriginalFilename();
        //得到后缀名
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //得到唯一文件名
        fileName = UUID.randomUUID()+suffix;
        //保存图片的路径

        File dirFile = new File(dirPath);
        //判断如果文件夹不存在 创建
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        //得到文件的完整路径
        String filePath = dirPath+"/"+fileName;
        //保存文件  异常抛出
        file.transferTo(new File(filePath));
        //保存文件名
        dwRareAnimal.setImagePath(fileName);
        return toAjax(dwRareAnimalService.insertDwRareAnimal(dwRareAnimal));
    }

    /**
     * 修改珍奇动物
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        DwRareAnimal dwRareAnimal = dwRareAnimalService.selectDwRareAnimalById(id);
        mmap.put("dwRareAnimal", dwRareAnimal);
        return prefix + "/edit";
    }

    /**
     * 修改保存珍奇动物
     */
    @RequiresPermissions("manage:animal:edit")
    @Log(title = "珍奇动物", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DwRareAnimal dwRareAnimal)
    {
        return toAjax(dwRareAnimalService.updateDwRareAnimal(dwRareAnimal));
    }

    /**
     * 删除珍奇动物
     */
    @RequiresPermissions("manage:animal:remove")
    @Log(title = "珍奇动物", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dwRareAnimalService.deleteDwRareAnimalByIds(ids));
    }
}
